<template>
	<div class="cart-container">
		<view class="cart-header">
			<text class="header-title">购物车</text>
			<text class="header-count">({{ cartItems.length }})</text>
		</view>
		<view class="cart-list">
			<view v-for="item in cartItems" :key="item.id" class="cart-item">
				<view class="item-info">
					<image :src="item.image" class="item-image" />
					<view class="item-details">
						<text class="item-name">{{ item.name }}</text>
						<text class="item-price">￥{{ item.price }} x {{ item.quantity }}</text>
					</view>
				</view>
				<view class="item-actions">
					<button class="remove-button" @click="removeFromCart(item.id)">删除</button>
				</view>
			</view>
		</view>
		<view class="cart-footer">
			<text class="total-price">合计: ￥{{ totalPrice }}</text>
			<button class="checkout-button" @click="checkout" @tap="jiesuan">结算</button>
		</view>
		<view v-if="cartItems.length === 0" class="empty-cart">购物车是空的</view>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				cartItems: [] // 购物车商品列表
			};
		},
		computed: {
			totalPrice() {
				return this.cartItems.reduce((total, item) => total + item.price * item.quantity, 0).toFixed(2);
			}
		},
		onShow() {
			const app = getApp(); // 获取全局应用实例
			this.cartItems = app.globalData.cartItems; // 从全局状态获取购物车商品
		},
		methods: {
			jiesuan(){
				uni.showToast({
					title:'已成功支付',
					icon: 'none'
				})
			},
			removeFromCart(itemId) {
				const app = getApp();
				app.globalData.cartItems = app.globalData.cartItems.filter(item => item.id !== itemId);
				this.cartItems = app.globalData.cartItems; // 更新本地购物车商品列表
			},
			checkout() {
				// 结算逻辑
				wx.showToast({
					title: '结算功能尚未实现',
					icon: 'none'
				});
			}
		}
	};
</script>

<style scoped>
	.cart-container {
		padding: 10px;
		background-color: #f9f9f9;
	}

	.cart-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px;
		background-color: #fff;
		border-bottom: 1px solid #e0e0e0;
	}

	.header-title {
		font-size: 18px;
		font-weight: bold;
	}

	.header-count {
		color: #999;
	}

	.cart-list {
		margin-top: 10px;
	}

	.cart-item {
		display: flex;
		justify-content: space-between;
		padding: 10px;
		background-color: #fff;
		border-bottom: 1px solid #e0e0e0;
	}

	.item-info {
		display: flex;
	}

	.item-image {
		width: 60px;
		height: 60px;
		margin-right: 10px;
	}

	.item-details {
		flex: 1;
	}

	.item-name {
		font-size: 16px;
		color: #333;
	}

	.item-price {
		font-size: 14px;
		color: #e60012;
	}

	.item-actions {
		display: flex;
		align-items: center;
	}

	.remove-button {
		background-color: #ff4d4f;
		color: white;
		border: none;
		width: 70px;
		height: 45px;
		margin-left: 10px;
		text-align: center;
		border-radius: 5px;
		cursor: pointer;
	}

	.cart-footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px;
		background-color: #fff;
		border-top: 1px solid #e0e0e0;
	}

	.total-price {
		font-size: 18px;
		color: #333;
	}

	.checkout-button {
		background-color: #007bff;
		color: white;
		width: 70px;
		height: 45px;
		text-align: center;
		border: none;
		/* padding: 10px 20px; */
		border-radius: 10px;
		cursor: pointer;
	}

	.empty-cart {
		text-align: center;
		color: #999;
		margin-top: 20px;
	}
</style>